package com.silence.springboot.service.impl;


import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.silence.springboot.configurations.properties.JwtProperties;
import com.silence.springboot.constants.StringConstants;
import com.silence.springboot.convert.LoginAdminConvert;
import com.silence.springboot.dao.LoginAdminDao;
import com.silence.springboot.dao.UserBaseInfoDao;
import com.silence.springboot.entity.LoginAdminEntity;
import com.silence.springboot.enums.ResultCode;
import com.silence.springboot.exception.ApiException;
import com.silence.springboot.mapper.LoginAdminMapper;
import com.silence.springboot.service.LoginAdminService;
import com.silence.springboot.utils.EncryptionDecryptionUtil;
import com.silence.springboot.utils.JwtUtil;
import com.silence.springboot.vo.input.LoginSystemInput;
import com.silence.springboot.vo.output.LoginSystemOutput;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 登录表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LoginAdminServiceImpl extends ServiceImpl<LoginAdminMapper, LoginAdminEntity> implements LoginAdminService {

    /**
     * 登录表 转换类
     */
    private final LoginAdminConvert loginConvert;

    /**
     * 登录表 数据访问层实现
     */
    private final LoginAdminDao loginAdminDao;

    /**
     * JWT配置类
     */
    private final JwtProperties jwtProperties;

    /**
     * 用户基础信息表 数据访问层接口
     */
    private final UserBaseInfoDao userBaseInfoDao;

    /**
     * 登录系统
     *
     * @param input 登录表入参
     * @return 登录表出参
     */
    @Override
    public LoginSystemOutput loginAdminSystem(LoginSystemInput input) {
        // 转换实体类
        LoginAdminEntity entity = loginConvert.loginSystemInputToLoginAdminEntity(input);
        // 获取用户名和密码
        String username = entity.getUsername();
        String password = entity.getPassword();
        log.info("用户名：{}，密码：{}", username, password);
        // 查询数据库中的用户名和密码
        LoginAdminEntity selectEntity = loginAdminDao.selectOneByQuery(username);
        // 判断用户名和密码是否正确
        if (Objects.isNull(selectEntity)) {
            throw new ApiException(ResultCode.LOGIN_USER_NOT_FOUND);
        }
        // 判断密码是否正确
        String sha256Str = EncryptionDecryptionUtil.getSha256Str(StringConstants.KEY.concat(password));
        log.info("SHA256加密后字符串：{}", sha256Str);
        if (!sha256Str.equals(selectEntity.getPassword())) {
            throw new ApiException(ResultCode.LOGIN_USER_PASSWORD_ERROR);
        }
        log.info("登录成功");
        String token = JwtUtil.createToken(System.currentTimeMillis(), selectEntity.getId());
        log.info("生成的 token 为：{}", token);
        userBaseInfoDao.updateTokenByUserId(selectEntity.getId(), jwtProperties.getTokenHeader().concat(token));
        // 构建出参
        return new LoginSystemOutput()
                .setAuthHeader(jwtProperties.getAuthHeader())
                .setExpiration(jwtProperties.getExpiration())
                .setTokenHeader(jwtProperties.getTokenHeader())
                .setToken(token);
    }
}